package com.ruoyi.hqfinance.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.hqfinance.domain.HqExportInfo;
import com.ruoyi.hqfinance.domain.HqNetworkBillDetail;
import com.ruoyi.hqfinance.domain.HqNetworkDetailCollect;
import com.ruoyi.hqfinance.domain.vo.HqNetworkBillDetailVo;
import com.ruoyi.hqfinance.domain.vo.HqNetworkBillPointVo;
import com.ruoyi.hqfinance.mapper.HqExportInfoMapper;
import com.ruoyi.hqfinance.mapper.HqNetworkBillDetailMapper;
import com.ruoyi.hqfinance.mapper.HqNetworkDetailCollectMapper;
import com.ruoyi.hqfinance.service.HqNetworkBillDetailService;
import com.ruoyi.logistic.domain.PageDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class HqNetworkBillDetailServiceImpl implements HqNetworkBillDetailService {
    @Autowired
    private HqNetworkBillDetailMapper hqNetworkBillDetailMapper;
    @Autowired
    private HqNetworkDetailCollectMapper hqNetworkDetailCollectMapper;
    @Autowired
    private HqExportInfoMapper hqExportInfoMapper;

    @Override
    public List<HqNetworkBillDetail> selectAll(PageDate pageDate) {
        PageHelper.startPage(pageDate);
        return hqNetworkBillDetailMapper.selectAll();
    }

    @Override
    public Boolean insert(HqNetworkBillDetail hqNetworkBillDetail) {
        hqNetworkBillDetail.setDeductPayDate(hqNetworkBillDetail.getDeductPayDate().replace("-", ""));
        hqNetworkBillDetail.setType("手工账单");
        return hqNetworkBillDetailMapper.insert(hqNetworkBillDetail);
    }

    @Override
    public Boolean update(HqNetworkBillDetail hqNetworkBillDetail) {
        return hqNetworkBillDetailMapper.update(hqNetworkBillDetail);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return hqNetworkBillDetailMapper.deleteById(id);
    }

    @Override
    public List<HqNetworkBillDetail> search(HqNetworkBillDetailVo hqNetworkBillDetailVo) {
        PageHelper.startPage(hqNetworkBillDetailVo.getPageDate());
        return hqNetworkBillDetailMapper.search(hqNetworkBillDetailVo);
    }

    @Override
    public HqNetworkDetailCollect collectAmount(HqNetworkBillDetailVo hqNetworkBillDetailVo) {
        return hqNetworkDetailCollectMapper.collectAmount(hqNetworkBillDetailVo);
    }

    @Override
    public List<HqNetworkBillDetail> point(HqNetworkBillPointVo vo) {
        PageHelper.startPage(vo.getPageDate());
        return hqNetworkBillDetailMapper.point(vo);
    }

    @Override
    public Boolean updateSetClient(HqNetworkBillDetail hqNetworkBillDetail) {
        return hqNetworkBillDetailMapper.updateSetClient(hqNetworkBillDetail);
    }

    @Override
    public Boolean updateSetContractArea(HqNetworkBillDetail hqNetworkBillDetail) {
        return hqNetworkBillDetailMapper.updateSetContractArea(hqNetworkBillDetail);
    }

    @Override
    @Async
    public R exportDetailAsync(HqNetworkBillDetailVo vo) {
        // 获取代码执行前的时间戳
        long startTime = System.currentTimeMillis();
        // 使用 UUID 生成唯一文件名
        String fileName = "网点账单明细_" + startTime + ".xlsx";
//        String path = "D:\\ruoyi\\uploadPath\\download\\" + "网点账单明细" +startTime + ".xlsx";
        String path = "/home/ruoyi/uploadPath/download/" + "网点账单明细" +startTime + ".xlsx";
        String sheetName = "网点账单明细";
        HqExportInfo hqExportInfo = new HqExportInfo();
        hqExportInfo.setSheetName("网点账单明细" + startTime + ".xlsx");
        hqExportInfo.setType("下载中");
//    hqExportInfo.setCreateBy(SecurityUtils.getUserId());
        hqExportInfoMapper.insert(hqExportInfo);

        List<HqNetworkBillDetail> lise = hqNetworkBillDetailMapper.search(vo);
        System.out.println("查询数据的耗时:" + (System.currentTimeMillis() - startTime));

        // 写入 Excel 文件到临时目录
        EasyExcel.write(path, HqNetworkBillDetail.class)
                .sheet(sheetName)
                .doWrite(lise);

        // 获取代码执行后的时间戳
        long endTime = System.currentTimeMillis();
        // 计算代码执行所需的毫秒数
        long executionTime = endTime - startTime;
        System.out.println("执行这段代码的耗时(毫秒)：" + executionTime);

        hqExportInfo.setType("已完成");
        hqExportInfo.setUrl(path);
        hqExportInfo.setId(hqExportInfoMapper.selectIdByName(hqExportInfo.getSheetName()));
        hqExportInfoMapper.update(hqExportInfo);

        // 返回文件信息给前端
        Map<String, Object> result = new HashMap<>();
        result.put("fileUrl", path);
        result.put("fileName", fileName);
        return R.ok(result);

    }
}
